﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DB.SqlHelper;
using Framework.Common.Common;
using Mode;

namespace Framework.DataAccess
{
   public  class ViewSubject
    {
        // 考场的信息
        private examination_table examination = null;
         /// <summary>
        /// 
        /// </summary>
        private readonly DbInstance db;
       /// <summary>
       /// 获得考试信息
       /// </summary>
        ExamDataManageEx edm = new ExamDataManageEx();
        // 应答者ID
        private int candidateID = -1;
        private int examid = -1;
        public ViewSubject(int candidate,int exmid)
        {
            this.db = DbManager.GetDbInstance();

            // 应答者ID
            this.candidateID = candidate;
            this.examid = exmid;
            

        }

        // 根据考场ID取得考场信息
        private void get_examination_by_id(int examID)
        {
            string sql = "SELECT * FROM examination_table WHERE id=" + examID;
            DataView view = db.ExecuteDataTableBySql(sql).DefaultView;


            if (view.Count != 1)
                throw new Exception("考场信息获取出错.\n[examination_table]:" + examID);

            examination = new examination_table();
            examination.id = view[0]["id"].ToInt();
            examination.name = view[0]["name"].ToString();
            examination.model = view[0]["model"].ToInt();
            examination.date = Convert.ToDateTime(view[0]["date"]);
            examination.begin = Convert.ToDateTime(view[0]["begin"]);
            examination.end = Convert.ToDateTime(view[0]["end"]);
            examination.time = view[0]["time"].ToInt();
            examination.pass = view[0]["pass"].ToInt();
            examination.status = view[0]["status"].ToInt();

            if (examination.status != 0)
                throw new Exception("考场已被禁用,请与管理员联系. [examination_table]:" + examination.id);
        }

        private void get_comminfo()
        {
            string strSql = "select id,name,sex,phone,department,workcode,email from member_table where id in(select top 1 member from candidate_table where id="+this.candidateID+")";
            DataView view = db.ExecuteDataTableBySql(strSql).DefaultView;
           // edm.comminfo = new ExamCommInfo();
            if (examination != null)
            {
                edm.comminfo.examname = examination.name;
                edm.comminfo.totaltime = examination.time;
            }

            if (view != null)
            {
                foreach (DataRowView dr in view)
                {

                    edm.comminfo.username = dr["name"].ToString();
                    edm.comminfo.phone = dr["phone"].ToString();
                    edm.comminfo.department = dr["department"].ToString();
                    edm.comminfo.workcode = dr["workcode"].ToString();
                    edm.comminfo.email = dr["email"].ToString();
                
                }
            
            }
        }

        private List<ExmTopicEx> get_topic_information(int topictype)
        {
            List<ExmTopicEx> result = new List<ExmTopicEx>();

            int mode = -1;
            if (examination != null) mode = examination.model;

            string strSql = string.Format("Select A.*,B.[full]As score from topic_table As A inner Join sheet_table As B On (A.id=B.topic) Where B.candidate={0} And B.topictype={1}", candidateID, topictype);
            //题目答案
           string strSqlAnswer = "SELECT * FROM ANSWER_TABLE WHERE TOPIC in (select topic from  sheet_table where candidate=" + candidateID + " and topictype=" + topictype + ") ";
           //做题答案select sheet,answerid,isnull(reply,'')reply from dbo.reply_table
           string strSqlAnswer2 = "SELECT (select top 1 topic from sheet_table where id=sheet)as topic,  sheet,answerid,isnull(reply,'')reply  FROM reply_table WHERE sheet in (select id  from  sheet_table where candidate=" + candidateID + " and topictype=" + topictype + ") ";
            //取题目得分
           string strsheetSql = "select * from  sheet_table where candidate=" + candidateID + " and topictype=" + topictype;
          
             DataView view = db.ExecuteDataTableBySql(strSql).DefaultView;

             DataView view2 = db.ExecuteDataTableBySql(strSqlAnswer).DefaultView;
             DataView view3 = db.ExecuteDataTableBySql(strSqlAnswer2).DefaultView;
             //
             DataView view4 = db.ExecuteDataTableBySql(strsheetSql).DefaultView;
             for (int index = 0; index < view.Count; index++)
             {
                 ExmTopicEx et = new ExmTopicEx();
                 et.id = view[index]["id"].ToInt();
                 et.title = view[index]["title"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                 et.type = topictype;
                 et.score = view[index]["score"].ToInt();
                 et.answer = new List<TopicAnswer>();
                 et.answer2 = new List<TopicAnswer>();

                 DataView tempscore = view4;
                 tempscore.RowFilter = "TOPIC=" + et.id;
                 if (tempscore.Count > 0)
                 {
                     et.scoreOK = tempscore[0]["score"].ToInt();
                 }
                 //打字题获得分数
                 if (topictype == 6)
                 {
                     
                    
                 }
                 else
                 {
                     DataView temp = view2;
                     temp.RowFilter = "TOPIC=" + et.id;
                     for (int i = 0; i < temp.Count; i++)
                     {
                         TopicAnswer top = new TopicAnswer();
                         top.id = temp[i]["id"].ToInt();
                         top.answer = temp[i]["answer"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                         top.code = temp[i]["code"].ToInt();
                         top.topic = temp[i]["topic"].ToInt();
                         top.right = temp[i]["right"].ToInt();

                         et.answer.Add(top);
                     }
                     DataView temp2 = view3;
                     temp2.RowFilter = "topic=" + et.id;
                     for (int i = 0; i < temp2.Count; i++)
                     {
                         TopicAnswer top = new TopicAnswer();
                         top.id = temp2[i]["answerid"].ToInt();
                         top.answer = temp2[i]["reply"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                         top.topic = temp2[i]["topic"].ToInt();
                         et.answer2.Add(top);
                     }
                 }
             
                 result.Add(et);

             }

             return result;
        }
       /// <summary>
       /// 获得考题
       /// </summary>
       /// <returns></returns>
        public ExamDataManageEx Generate()
        {
            //考场信息
            get_examination_by_id(examid);
            edm.comminfo = new ExamCommInfo();
            get_comminfo();

                for (int index = 1; index < 7; index++)
                {
                    List<ExmTopicEx> Temp = get_topic_information(index);

                    switch (index)
                    {
                        case 1:
                            edm.topicLen1 = Temp.Count;

                            edm.topic1 = Temp;
                            break;
                        case 2:
                            edm.topicLen2 = Temp.Count;
                            edm.topic2 = Temp;
                            break;
                        case 3:
                            edm.topicLen3 = Temp.Count;
                            edm.topic3 = Temp;
                            break;
                        case 4:
                            edm.topicLen4 = Temp.Count;
                            edm.topic4 = Temp;
                            break;
                        case 5:
                            edm.topicLen5 = Temp.Count;
                            edm.topic5 = Temp;
                            break;
                        case 6:
                            edm.topicLen6 = Temp.Count;
                            edm.topic6 = Temp;
                            break;

                    }
                }
                
     
            return edm;
        }


     

    }
}
